package com.im.dispatcher;

import io.netty.channel.socket.SocketChannel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Created leijun
 * @Date 2021/9/12 8:28 下午
 */
public class GatewayInstanceManager {

    private GatewayInstanceManager() {}

    static class Singleton{
        private static GatewayInstanceManager instance = new GatewayInstanceManager();
    }

    public static GatewayInstanceManager getInstance() {
        return Singleton.instance;
    }

    private Map<String, SocketChannel> channels = new ConcurrentHashMap<>();

    public void addInstance(SocketChannel socketChannel) {
        String channelId = socketChannel.remoteAddress().getHostName() + ":" + socketChannel.remoteAddress().getPort();
        channels.put(channelId, socketChannel);
    }

    public void remove(SocketChannel socketChannel) {
        channels.remove(socketChannel.remoteAddress().getHostString());
    }

    public SocketChannel getGatewayInstance(String channelId) {
        return channels.get(channelId);
    }
}
